<?php

namespace addons\shopro\controller;


use addons\shopro\service\user\User as UserService;
use addons\shopro\service\Wallet as WalletService;
use app\admin\model\shopro\user\WalletLog;
use app\admin\model\user\Prizeappoin;
use app\admin\model\user\Prizelog;
use app\admin\model\user\Userfinancial;
use app\common\model\shopro\UserNormalPrizeLogModel;
use app\common\model\shopro\UserTeamDayWalletLogModel;
use app\common\model\Userchildren;
use think\Db;

class Prize extends Common
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * @menu:奖品列表
     */
    public function index()
    {
        $model = new \app\admin\model\shopro\goods\Prize();
        $data  = $model->order('id desc')->field('id,title')->select();
        $this->success('ok', [
            'data'            => $data,
            'max_prize_money' => config('site.max_prize_money'),
            'prize_money'     => config('site.prize_money')
        ]);
    }

    /**
     * @menu:砸金蛋
     * @Author:男人的坏
     * @Date:2024-07-18 21:45
     */
    public function draw()
    {
        $user = auth_user();
        if (config('site.licaikaiguan')) {
            $count = Userfinancial::where(['user_id' => $this->auth->id])->count();
            if (!$count) {
                $this->error('请先购买理财产品');
            }
        }
        $type = input('type');
        if (!in_array($type, [1, 2])) {
            $this->error('参数错误');
        }
        if ($type == 1) {
            $prize_money = config('site.max_prize_money');
        } else {
            $prize_money = config('site.prize_money');
        }
        if ($user->score < $prize_money) {
            $this->error('绿色积分不足');
        }
        $info = Db::transaction(function () use ($user, $prize_money) {
            $principal         = $prize_money;
            $prizeAppointModel = new Prizeappoin();
            $prizeAppointInfo  = $prizeAppointModel->where([
                'user_id' => $user->id,
                'status'  => 0
            ])->order('id asc')->find();
            if ($prizeAppointInfo) {
                $prizeAppointInfo->status = 1;
                $prizeAppointInfo->save();
                $info = \app\admin\model\shopro\goods\Prize::where(['id' => $prizeAppointInfo->prize_id])->field('id,type,title,chance,already_chance')->find();
            } else {
                $info = $this->digui();
            }
            WalletService::change($user->id, 'score', -$prize_money, 'draw', [],
                '砸金蛋');
            Prizelog::create(['user_id' => $user->id, 'prize_id' => $info->id, 'principal' => $principal,'day_time'=>time()+(86400*$info->day)]);
            return $info;
        });
        \think\Queue::push('\addons\shopro\job\PrizePaid@paid',
            ['user' => $user, 'prize_money' => $prize_money, 'prizeAppointInfo' => $info], 'shopro-high');
        $this->success('恭喜中奖', $info);
    }


    /**
     * @menu:普通会员奖励
     * @Author:男人的坏
     * @Date:2024-07-20 11:49
     */

    public function user_normal($user, $prize_money, $prizeAppointInfo, $userModel)
    {
        $time             = date('Ymd');
        $user_create_time = date('Ymd', $user['createtime']);
        $count            = $userModel->where(['parent_user_id' => $user['parent_user_id']])->count();
        if ($count >= 3 && $user['level'] == 0 && $time != $user_create_time) {
            $money = bcmul($prize_money, config('site.game_dividend') / 100);
            if ($money) {
                WalletService::change($user['parent_user_id'], 'score', $money, 'game_dividend', [],
                    '团队游戏流水返利');
            }
            if ($prizeAppointInfo['type'] == 2) {
                $inser['user_id']      = $user['parent_user_id'];
                $inser['draw_user_id'] = $user['id'];
                $inser['prize_id']     = $prizeAppointInfo['id'];
                UserNormalPrizeLogModel::create($inser);
            }
        }
    }


    public function digui($ids = 0)
    {
        $model          = new \app\admin\model\shopro\goods\Prize();
        $already_chance = $model->sum('already_chance');
        if ($already_chance >= 100) {
            $model->where('id', '>', 0)->update(['already_chance' => 0]);
        }
        if ($ids) {
            $model = $model->where('id', '<>', $ids);
        }
        $info = $model->where('chance','>',0)->orderRaw('rand()')->field('id,type,title,chance,already_chance,day')->find();
        if ($info->chance == $info->already_chance) {
            return $this->digui($info->id);
        }
        $info->setInc('already_chance');
        return $info;
    }

    /**
     * @menu:中奖记录
     * @Author:男人的坏
     * @Date:2024-07-19 20:44
     */
    public function prize_log()
    {
        $model = new Prizelog();
        $type  = input('type');
        $query = $model->with(['prize'])->where(['user_id' => $this->auth->id]);
        if ($type) {
            $query = $query->where(['prize.type' => $type]);
        }
        $data = $query->order('id desc')->paginate();
        if ($data) {
            foreach ($data as $row) {
                $row->visible(['id', 'status', 'principal', 'createtime','day_time']);
                $row->visible(['prize']);
                $row->getRelation('prize')->visible(['type', 'title', 'num']);
            }
        }
        $this->success('ok', $data);
    }

    /**
     * @menu:中奖明细
     */
    public function user_prize_log()
    {
        $model = new WalletLog();
        $ids   = input('ids');
        if (!$ids) {
            $this->error('缺少参数');
        }
        $data = $model->where('event', 'prize')->where([
            'user_id' => $this->auth->id,
            'ext'     => $ids,
            'type'    => 'score'
        ])->paginate();
        $this->success('ok', $data);
    }

}
